package com.zabra.zl.ui.index_fragment.account;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpFragment;
import com.zabra.zl.data.AccountBean;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.data.GridData;
import com.zabra.zl.ui.about.AboutActivity;
import com.zabra.zl.ui.allocation_manage.AllocationManageActivity;
import com.zabra.zl.ui.deposit.DepositActivity;
import com.zabra.zl.ui.generalize.GeneralizeActivity;
import com.zabra.zl.ui.index_fragment.account.contract.AccountFragmentContract;
import com.zabra.zl.ui.index_fragment.account.presenter.AccountFragmentPresenter;
import com.zabra.zl.ui.nullpage.NullPersonActivity;
import com.zabra.zl.ui.personal_center.PersonalActivity;
import com.zabra.zl.ui.recharge.RechargeActivity;
import com.zabra.zl.ui.runing.RunningActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.HideUtil;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class AccountFragment extends BaseMvpFragment<AccountFragmentPresenter> implements AccountFragmentContract.View {
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_security)
    TextView tvSecurity;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.ll_recharge)
    LinearLayout llRecharge;
    @BindView(R.id.ll_deposit)
    LinearLayout llDeposit;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.ll_dot)
    LinearLayout llDot;
    @BindView(R.id.icon1)
    ImageView icon1;
    @BindView(R.id.rl_running)
    RelativeLayout rlRunning;
    @BindView(R.id.icon2)
    ImageView icon2;
    @BindView(R.id.rl_manage)
    RelativeLayout rlManage;
    @BindView(R.id.icon3)
    ImageView icon3;
    @BindView(R.id.rl_generalize)
    RelativeLayout rlGeneralize;
    @BindView(R.id.icon4)
    ImageView icon4;
    @BindView(R.id.rl_about)
    RelativeLayout rlAbout;
    Unbinder unbinder;
    private LinearLayout mLlDot;
    private LayoutInflater inflater;
    private List<View> mPagerList;
    private List<GridData> mData;
    private View[] views;
    private LinearLayout.LayoutParams layoutParams;
    private Bundle bundle=new Bundle();
    private View view;
    private Dialog dialog;
    private int realName;
    private String strBlance;
    /**
     * 总的页数
     */
    private int pageCount;
    /**
     * 每一页显示的个数
     */
    private int pageSize = 3;
    /**
     * 当前显示的是第几页
     */
    private int curIndex = 0;

    @Override
    protected void initView(View view) {
        mPresenter = new AccountFragmentPresenter();
        mPresenter.attachView(this);
        keyList.add("debtorId");
        valueList.add(strUserid);

        mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_account;
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(getActivity());
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(AccountBean bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(getActivity()).putSP("token", token);
            SharedPreferencesUtil.getInstance(getActivity()).putSP("tel", bean.getTel());
            strBlance=bean.getCashBalance();
            //1实名，0未实名
            realName = bean.getAttestation();
            tvAccount.setText("账户 " + HideUtil.hideMobile(bean.getTel()));
            tvAll.setText(bean.getTotBalance());
            tvSecurity.setText(bean.getSecurityBalance());
            tvBalance.setText(strBlance);
            initDatas(bean);
            inflater = LayoutInflater.from(getActivity());
            //总的页数=总数/每页数量，并取整
            pageCount = (int) Math.ceil(mData.size() * 1.0 / pageSize);
            mPagerList = new ArrayList<View>();
            for (int i = 0; i < pageCount; i++) {
                // 每个页面都是inflate出一个新实例
                GridView gridView = (GridView) View.inflate(getActivity(), R.layout.gridview, null);
                gridView.setAdapter(new GridViewAdapter(getActivity(), mData, i, pageSize));
//                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        int pos = position + curIndex * pageSize;
//
//                    }
//                });
                mPagerList.add(gridView);
            }
            viewpager.setAdapter(new ViewPagerAdapter(mPagerList));
//            setOvalLayout();
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(getActivity());
        } else {
            ToastUtil.showToast(getActivity(), bean.getRespDesc());
        }
    }


    @OnClick({R.id.tv_account, R.id.ll_recharge, R.id.ll_deposit, R.id.rl_running, R.id.rl_manage, R.id.rl_generalize, R.id.rl_about})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_account:
                //账户信息
                toClass1(getActivity(), PersonalActivity.class);
                break;
            case R.id.ll_recharge:
                //充值
                bundle.putString("blance",strBlance);
                toClass(getActivity(), RechargeActivity.class,bundle);
                break;
            case R.id.ll_deposit:
                //提现
                if (realName == 0) {
                    EventMessage message = new EventMessage(1, "Main");
                    EventBus.getDefault().postSticky(message);
                    toClass1(getActivity(), NullPersonActivity.class);
                } else {
                    toClass(getActivity(), DepositActivity.class);
                }
                break;
            case R.id.rl_running:
                //流水
                toClass(getActivity(), RunningActivity.class);
                break;
            case R.id.rl_manage:
                //管理
                if (realName == 0) {
                    EventMessage message = new EventMessage(1, "Main");
                    EventBus.getDefault().postSticky(message);
                    toClass1(getActivity(), NullPersonActivity.class);
                } else {
                    toClass(getActivity(), AllocationManageActivity.class);
                }

                break;
            case R.id.rl_generalize:
                //推广
                toClass(getActivity(), GeneralizeActivity.class);
                break;
            case R.id.rl_about:
                //关于
                toClass1(getActivity(), AboutActivity.class);
                break;
        }
    }

    private void initDatas(AccountBean bean) {
        mData = new ArrayList<>();
        GridData data1 = new GridData();
        GridData data2 = new GridData();
        GridData data3 = new GridData();

        data1.setName("持仓市值");
        data1.setMoney(bean.getPositionBalance());
        data1.setIcon(R.mipmap.grid1);

        data2.setName("可投余额");
        data2.setMoney(bean.getUseBalance());
        data2.setIcon(R.mipmap.grid2);

        data3.setName("浮动盈亏");
        data3.setMoney(bean.getEarnBalance());
        data3.setIcon(R.mipmap.grid3);

        mData.add(data1);
        mData.add(data2);
        mData.add(data3);

    }

    /**
     * 设置圆点
     */
    public void setOvalLayout() {
        views = new View[pageCount];
        for (int i = 0; i < pageCount; i++) {
            views[i] = new View(getActivity());
            view = new View(getActivity());
            layoutParams = new LinearLayout.LayoutParams(8, 8);
            if (i == 0) {
                views[i] = view;
                views[i].setBackgroundResource(R.drawable.dot_pitch);
            } else {
                views[i] = view;
                views[i].setBackgroundResource(R.drawable.dot_nopitch);
            }
            layoutParams.leftMargin = 10;
            layoutParams.rightMargin = 10;
            view.setLayoutParams(layoutParams);
            views[i].setPadding(8, 8, 8, 8);
            llDot.addView(views[i]);
        }
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < pageCount; i++) {
                    if (i == position) {
                        views[i].setBackgroundResource(R.drawable.dot_pitch);
                    } else {
                        views[i].setBackgroundResource(R.drawable.dot_nopitch);
                    }

                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

}
